package main

import (
    "log"
    "net/http"
    "fmt"
    "github.com/prometheus/client_golang/prometheus/promhttp"
    "github.com/prometheus/client_golang/prometheus"
)

var (
	// step1：初始化一个counter
	pushCounter = prometheus.NewCounter(prometheus.CounterOpts{
	    Name: "demo_request_total",
	    Help: "request total time",
	})
)

func init() {
	fmt.Println("init")
	//step2: 注册容器
	err := prometheus.Register(pushCounter)
	if err != nil {
	    fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err)
	    return
	}

}

func main() {
    http.Handle("/metrics", promhttp.Handler())
    http.HandleFunc("/demo", myHandler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func myHandler(w http.ResponseWriter, r *http.Request) {
	pushCounter.Inc()
	fmt.Println(22222)
	w.Write([]byte("Hello Golang"))
}
